from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession

from module_stock.entity.do.info_do import StockInfo
from module_stock.entity.do.stat_position_do import StatPosition
from module_stock.entity.vo.stat_position_vo import StatPositionModel, StatPositionPageQueryModel
from utils.page_util import PageUtil


class StatPositionDao:
    """
    股票交易统计模块数据库操作层
    """

    @classmethod
    async def get_stat_position_detail_by_id(cls, db: AsyncSession, trade_date: int):
        """
        根据交易日期获取股票交易统计详细信息

        :param db: orm对象
        :param trade_date: 交易日期
        :return: 股票交易统计信息对象
        """
        stat_position_info = (
            (
                await db.execute(
                    select(StatPosition)
                    .where(
                        StatPosition.trade_date == trade_date
                    )
                )
            )
            .scalars()
            .first()
        )

        return stat_position_info

    @classmethod
    async def get_stat_position_detail_by_info(cls, db: AsyncSession, stat_position: StatPositionModel):
        """
        根据股票交易统计参数获取股票交易统计信息

        :param db: orm对象
        :param stat_position: 股票交易统计参数对象
        :return: 股票交易统计信息对象
        """
        stat_position_info = (
            (
                await db.execute(
                    select(StatPosition).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return stat_position_info

    @classmethod
    async def get_stat_position_list(cls, db: AsyncSession, query_object: StatPositionPageQueryModel,
                                     is_page: bool = False):
        """
        根据查询参数获取股票交易统计列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 股票交易统计列表信息对象
        """
        query = (
            select(StatPosition, StockInfo.stock_name)
            .join(StockInfo, StatPosition.stock_code == StockInfo.stock_code, isouter=True)
            .where(
                StatPosition.trade_date == query_object.trade_date if query_object.trade_date else True,
                StatPosition.account_tag == query_object.account_tag if query_object.account_tag else True,
                StatPosition.stock_code == query_object.stock_code if query_object.stock_code else True,
                StatPosition.stock_name.like(f'%{query_object.stock_name}%') if query_object.stock_name else True,
            )
            .order_by(StatPosition.trade_date)
            .distinct()
        )
        stat_position_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return stat_position_list

    @classmethod
    async def add_stat_position_dao(cls, db: AsyncSession, stat_position: StatPositionModel):
        """
        新增股票交易统计数据库操作

        :param db: orm对象
        :param stat_position: 股票交易统计对象
        :return:
        """
        db_stat_position = StatPosition(**stat_position.model_dump(exclude={}))
        db.add(db_stat_position)
        await db.flush()

        return db_stat_position

    @classmethod
    async def edit_stat_position_dao(cls, db: AsyncSession, stat_position: dict):
        """
        编辑股票交易统计数据库操作

        :param db: orm对象
        :param stat_position: 需要更新的股票交易统计字典
        :return:
        """
        await db.execute(update(StatPosition), [stat_position])

    @classmethod
    async def delete_stat_position_dao(cls, db: AsyncSession, stat_position: StatPositionModel):
        """
        删除股票交易统计数据库操作

        :param db: orm对象
        :param stat_position: 股票交易统计对象
        :return:
        """
        await db.execute(delete(StatPosition).where(StatPosition.trade_date.in_([stat_position.trade_date])))

    @classmethod
    async def clear_stat_position_dao(cls, db: AsyncSession, trade_date: str):
        """
        清空指定日期股票持仓数据数据库操作
        目前先全都清空！！！
        :param db: orm对象
        :param trade_date: 删除当日持仓统计数据
        :return:
        """
        await db.execute(
            delete(StatPosition)
            # .where(StatPosition.trade_date == datetime.strptime(trade_date, '%Y-%m-%d'))
        )
